package main;

import javax.swing.table.AbstractTableModel;

public class AISTableModel extends AbstractTableModel{

	private String[] COLUMN_NAME = {"ID", "X", "Y", "W", "H", "Misc"};
	private Object[][] DATA = new Object[0][COLUMN_NAME.length];
	
	public int getColumnCount() {
		return COLUMN_NAME.length;
	}

	public int getRowCount() {
		return DATA.length;
	}

	public Object getValueAt(int row, int col) {
		if(row != -1){
			return DATA[row][col];
		}
		return null;
	}
	
	public String getColumnName(int col){
		return COLUMN_NAME[col];
	}
	
	public boolean isCellEditable(int row, int col) {
		if (getColumnName(col).equals(COLUMN_NAME[0])) {
			return false;
	    } else {
	    	return true;
	   }
	}
	
	public void setValueAt(Object value, int row, int col) {
		if(row == -1){
			return;
		}
        DATA[row][col] = value;
        if(getColumnName(col).equals("Misc")){
        	AthenaMain.manager.moduleUpdate(row, col, value.toString());
        } else {
        	AthenaMain.manager.moduleUpdate(row, col, Integer.parseInt(value.toString()));
        }
        fireTableCellUpdated(row, col);
    }

	public void addRow(Object[] moduleData) {
		Object[][] tmp = new Object[DATA.length + 1][];
		int i;
		for(i = 0; i < DATA.length; i++){
			tmp[i] = DATA[i];
		}
		tmp[i] = moduleData;
		DATA = tmp;
	}

	public void removeRow(int row){
		if(row == -1){
			return;
		}
		Object[][] tmp = new Object[DATA.length - 1][];
		for(int i = 0, j = 0; i < DATA.length; i++){
			if(i != row){
				tmp[j] = DATA[i];
				j++;
			}
		}
		DATA = tmp;
		updateID();
	}

	public void moveRow(int row, boolean b){
		if(row == -1){
			return;
		}
		Object[][] tmp = new Object[DATA.length][];
		for(int i = 0; i < DATA.length; i++){
			if(i == row){
				if(b){
					tmp[i - 1] = DATA[i];
					tmp[i] = DATA[i - 1];
				} else {
					tmp[i + 1] = DATA[i];
					tmp[i] = DATA[i + 1];
					i++;
				}
			} else {
				tmp[i] = DATA[i];
			}
		}
		DATA = tmp;
		updateID();
	}
	
	public void updateID(){
		for(int i = 0; i < DATA.length; i++){
			DATA[i][0] = i;
		}
	}
}
